package DataAccess;

import BussinesLogic.Request;
import java.sql.*;

public class driverPGSQL {
    private Connect conexion;
    
    public driverPGSQL(Connect conn) {
        this.conexion = conn;
    }
    
    public Request querySelect(String SQL){        
        Request req = new Request();
        try{
            Statement s = conexion.conn.createStatement();
            req.setRequest(s.executeQuery (SQL));
            req.setTransac(true);
            req.setMessage("query completed");
        }
        catch(SQLException ex){
            req.setTransac(false);
            req.setRequest(null);
            req.setMessage(ex.getMessage());
        }
        return req;
    }
    
    public Request queryInsert(String SQL){
        Request req = new Request();
        try{
            Statement stm = conexion.conn.createStatement();
            stm.executeUpdate(SQL);
            req.setTransac(true);
            req.setMessage("registrado");
        } catch(SQLException ex){
            req.setTransac(false);
            req.setMessage(ex.getMessage());
        }
        return req;
    }
    
    public Request queryUpdate(String SQL) {
        Request req = new Request();
        try{
            Statement s = conexion.conn.createStatement();
            int numFilas = s.executeUpdate(SQL);
            if(numFilas==1){
                req.setTransac(true);
                req.setMessage("actualizado");
            }
        else new SQLException();
        }
        catch(SQLException ex){
            req.setTransac(false);
            req.setMessage(ex.getMessage());
        }
        return req;  
    }
    
    public Request queryDelete(String SQL) {
        Request req = new Request();
        try{
            Statement s = conexion.conn.createStatement();
            int numFilas = s.executeUpdate(SQL);
            if(numFilas==1){
                req.setTransac(true);
                req.setMessage("eliminado");
            }
            else new SQLException();
        }
        catch(SQLException ex){
            req.setTransac(false);
            req.setMessage(ex.getMessage());
        }
        return req;
    }
}
